﻿using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace UFrame
{
    public class PLChangeFont : EditorWindow
    {
        [MenuItem("Plateface/UGUI/ChangeFont", false, 1)]
        public static void Open()
        {
            EditorWindow.GetWindow(typeof(PLChangeFont), true);
        }

        //public Font toChange = new Font("Arial");
        public Font toChange;
        static Font toChangeFont;

        void OnGUI()
        {
            toChange = (Font)EditorGUILayout.ObjectField("选择需要更换的字体", toChange, typeof(Font), true,
                GUILayout.MinWidth(100));
            toChangeFont = toChange;
            if (GUILayout.Button("确认更换"))
            {
                Change();
            }
        }

        public static void Change()
        {
            Object[] Texts = Selection.GetFiltered(typeof(Text), SelectionMode.Deep);
            foreach (Object text in Texts)
            {
                if (text)
                {
                    //如果是UGUI讲UILabel换成Text就可以  
                    Text TempText = (Text)text;
                    Undo.RecordObject(TempText, TempText.gameObject.name);
                    TempText.font = toChangeFont;
                    Debug.Log(text.name + ":" + TempText.text);
                    EditorUtility.SetDirty(TempText);
                }
            }
        }
    }
}